package cn.wolfcode.luowowo.advice;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* 下面这个标签可以实现全局异常处理
* */
@ControllerAdvice
public class UnifyExceptionHandler {
    //@ExceptionHandler这个注解可以用来统一处理方法抛出的异常
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object commonExp(Exception e, HttpServletResponse response) throws IOException {
        e.printStackTrace();
        return JsonResult.error(JsonResult.CODE_ERROR, e.getMessage(), null);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object runTimeException(Exception e ,HttpServletResponse response)throws IOException{
        e.printStackTrace();
        return JsonResult.defaultError();
    }
}
